package hos.thread.executor

import hos.thread.RunnableJob
import hos.thread.RunnableLife

/**
 * <p>Title: CallBackground </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2023/8/10 18:11
 * @version : 1.0
 */
abstract class CallBackground<T>(onBackground: RunnableJob<T>.() -> T) :
    RunnableJob<T>(onBackground) {

    init {
        onSuccess {
            onSuccess(this, it)
        }.onStart {
            onStart(this)
        }.onCancel {
            onCancel()
        }.onProgress {
            onProgress(this)
        }.onCatch {
            onThrowable(this, it)
        }
    }

    /**
     * 执行完成
     *
     * @param result 返回结果
     */
    abstract fun onSuccess(runnableLife: RunnableJob<T>, result: T?)

    open fun onStart(runnableLife: RunnableLife) {}

    open fun onCancel() {}

    open fun onProgress(value: Double) {}

    open fun onThrowable(throwable: Throwable, threadName: String) {}
}